﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using MySql.Data.MySqlClient;
using CodeRobot.Utility;
using System.Windows.Forms;

namespace CodeRobot.DAL
{
    /// <summary>
    /// .net core处理请求类
    /// </summary>
    public class JavaControllersHelper
    {

        /// <summary>
        /// 根据表名创建Controllers类
        /// </summary>
        /// <param name="strFilePath">保存路径</param>
        /// <param name="strProjectName">项目名</param>
        /// <param name="strTableName">表名</param>
        public static void CreateControllersClass(string strFilePath,string strProjectName, string strTableName,string strTableComment)
        {
            //读取版权信息
            CodeRobot.Utility.IniFile iniFile = new Utility.IniFile(Application.StartupPath + "\\config.ini");
            string strCompany = iniFile.GetString("COPYRIGHT", "COMPANY", "");
            string strAuthor = iniFile.GetString("COPYRIGHT", "AUTHOR", "");
            string strVersion = iniFile.GetString("COPYRIGHT", "VERSION", "");
            string strCode = iniFile.GetString("COPYRIGHT", "CODE", "");
            string strCreateDate = iniFile.GetString("BASE", "CREATE_DATE", "");

            string strTableNameUpper = CommonHelper.GetTableNameUpper(strTableName);
            
            string strPrimaryKey = CommonHelper.GetPrimaryKey(strTableName);//如：news_id
            string strPrimaryKeyDataType = CommonHelper.GetPrimaryKeyDataType(strTableName);//如：news_id
            string strJavaDataType = CodeRobot.Utility.StringHelper.GetJavaDBType(strPrimaryKeyDataType);

            string strAllColumnName = CommonHelper.GetAllColumnName(strTableName);
            string strAllColumnNameNotKey = CommonHelper.GetAllColumnNameNotKey(strTableName);

            string strClassName = CommonHelper.GetClassName(strTableName);//类名
            strClassName = CommonHelper.GetTableNameUpper(strClassName);//News
            string strTableNameSpec = CommonHelper.GetTableNameFirstLowerSecondUpper(strClassName);//如：newsTypes
            string strTableNameLower = strTableNameSpec.ToLower();//如：newstypes

            string strNewPath = strFilePath + "\\" + strTableNameLower + "\\controller";
            if (!Directory.Exists(strNewPath))
            {
                Directory.CreateDirectory(strNewPath);
            }

            var utf8WithoutBom = new System.Text.UTF8Encoding(false);
            StreamWriter sw = new StreamWriter(strNewPath + "\\" + strClassName + "Controller.java", false, utf8WithoutBom);

            sw.WriteLine("/*");
            sw.WriteLine(" * Copyright " + DateTime.Now.Year + " Cloudin.");
            sw.WriteLine(" *");
            sw.WriteLine(" * Licensed under the Apache License, Version 2.0 (the \"License\");");
            sw.WriteLine(" * you may not use this file except in compliance with the License.");
            sw.WriteLine(" * You may obtain a copy of the License at");
            sw.WriteLine(" *");
            sw.WriteLine(" *      http://www.apache.org/licenses/LICENSE-2.0");
            sw.WriteLine(" *");
            sw.WriteLine(" * Unless required by applicable law or agreed to in writing, software");
            sw.WriteLine(" * distributed under the License is distributed on an \"AS IS\" BASIS,");
            sw.WriteLine(" * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.");
            sw.WriteLine(" * See the License for the specific language governing permissions and");
            sw.WriteLine(" * limitations under the License.");
            sw.WriteLine(" */");
            sw.WriteLine("package com.toonan." + strTableNameLower + ".controller;");
            sw.WriteLine("");
            sw.WriteLine("import org.springframework.web.bind.annotation.*;");
            sw.WriteLine("import org.springframework.beans.factory.annotation.Autowired;");
            sw.WriteLine("import com.baomidou.mybatisplus.plugins.Page;");
            sw.WriteLine("import com.toonan.core.matatype.Dto;");
            sw.WriteLine("import com.toonan.core.matatype.Dtos;");
            sw.WriteLine("import com.toonan.core.util.WebplusFormater;");
            sw.WriteLine("import com.toonan.core.vo.R;");
            sw.WriteLine("import com.toonan.core.cache.WebplusCache;");
            sw.WriteLine("import java.util.List;");
            sw.WriteLine("import com.toonan." + strTableNameLower + ".model." + strClassName + ";");
            sw.WriteLine("import com.toonan." + strTableNameLower + ".service." + strClassName + "Service;");
            sw.WriteLine("import org.springframework.stereotype.Controller;");
            sw.WriteLine("import com.toonan.core.web.BaseController;");
            sw.WriteLine("");
            sw.WriteLine("");
            sw.WriteLine("/**");
            sw.WriteLine(" * " + strTableComment + "控制器");
            sw.WriteLine(" * @Author Cloudin");
            sw.WriteLine(" * @CreateDate " + Convert.ToDateTime(strCreateDate).ToString("yyyy年M月d日"));
            sw.WriteLine(" * @UpdateDate " + DateTime.Now.ToString("yyyy年M月d日"));
            sw.WriteLine(" * @Version V" + strVersion + "." + strCode);
            sw.WriteLine(" */");
            sw.WriteLine("@Controller");
            sw.WriteLine("@RequestMapping(\"/" + strTableNameSpec + "\")");
            sw.WriteLine("public class " + strClassName + "Controller extends BaseController {");
            sw.WriteLine("");
            sw.WriteLine("	@Autowired");
            sw.WriteLine("	private " + strClassName + "Service " + strTableNameLower + "Service;");
            sw.WriteLine("");
            sw.WriteLine("	/**");
            sw.WriteLine("	 * 获取列表数据");
            sw.WriteLine("	 * @return");
            sw.WriteLine("	 */");
            sw.WriteLine("	@RequestMapping(\"list\")");
            sw.WriteLine("	@ResponseBody");
            sw.WriteLine("	public R list() {");
            sw.WriteLine("		Dto pDto = Dtos.newDto(request);");
            sw.WriteLine("		pDto.setOrder(\" "+strPrimaryKey+" DESC \");");
            sw.WriteLine("		Page<" + strClassName + "> page =" + strTableNameLower + "Service.likePage(pDto);");
            sw.WriteLine("		WebplusCache.convertItem(page);");
            sw.WriteLine("		return R.toPage(page);");
            sw.WriteLine("	}");
            sw.WriteLine("");
            sw.WriteLine("	/**");
            sw.WriteLine("	 * 新增新闻类型");
            sw.WriteLine("	 * @param " + strTableNameLower + "");
            sw.WriteLine("	 * @return");
            sw.WriteLine("	 */");
            sw.WriteLine("	@PostMapping(\"save\")");
            sw.WriteLine("	@ResponseBody");
            sw.WriteLine("	public R save(" + strClassName + " " + strTableNameLower + ") {");
            sw.WriteLine("		boolean result = " + strTableNameLower + "Service.insert(" + strTableNameLower + ");");
            sw.WriteLine("		if (result) {");
            sw.WriteLine("			return R.ok();");
            sw.WriteLine("		} else {");
            sw.WriteLine("			return R.error(\"保存失败\");");
            sw.WriteLine("		}");
            sw.WriteLine("	}");
            sw.WriteLine("");
            sw.WriteLine("	/**");
            sw.WriteLine("	 * 根据ID编辑新闻类型字段数据");
            sw.WriteLine("	 * @param id");
            sw.WriteLine("	 * @return");
            sw.WriteLine("	 */");
            sw.WriteLine("	@PostMapping(\"edit\")");
            sw.WriteLine("	@ResponseBody");
            sw.WriteLine("	public R edit(String id) {");
            sw.WriteLine("		" + strClassName + " " + strTableNameLower + "=" + strTableNameLower + "Service.selectById(id);");
            sw.WriteLine("		return R.toData(" + strTableNameLower + ");");
            sw.WriteLine("	}");
            sw.WriteLine("");
            sw.WriteLine("	/**");
            sw.WriteLine("	 * 更新新闻类型数据");
            sw.WriteLine("	 * @param " + strTableNameLower + "");
            sw.WriteLine("	 * @return");
            sw.WriteLine("	 */");
            sw.WriteLine("	@PostMapping(\"update\")");
            sw.WriteLine("	@ResponseBody");
            sw.WriteLine("	public R update(" + strClassName + " " + strTableNameLower + ") {");
            sw.WriteLine("		boolean result = " + strTableNameLower + "Service.updateById(" + strTableNameLower + ");");
            sw.WriteLine("		if (result) {");
            sw.WriteLine("			return R.ok();");
            sw.WriteLine("		} else {");
            sw.WriteLine("			return R.error(\"更新失败\");");
            sw.WriteLine("		}");
            sw.WriteLine("	}");
            sw.WriteLine("");
            sw.WriteLine("	/**");
            sw.WriteLine("	 * 根据ID删除新闻类型");
            sw.WriteLine("	 * @param id");
            sw.WriteLine("	 * @return");
            sw.WriteLine("	 */");
            sw.WriteLine("	@PostMapping(\"remove\")");
            sw.WriteLine("	@ResponseBody");
            sw.WriteLine("	public R remove("+ strJavaDataType + " id) {");
            sw.WriteLine("		boolean result = " + strTableNameLower + "Service.deleteById(id);");
            sw.WriteLine("		if (result) {");
            sw.WriteLine("			return R.ok();");
            sw.WriteLine("		} else {");
            sw.WriteLine("			return R.error(\"删除失败\");");
            sw.WriteLine("		}");
            sw.WriteLine("	}");
            sw.WriteLine("");
            sw.WriteLine("	/**");
            sw.WriteLine("	 * 根据IDS批量删除新闻类型");
            sw.WriteLine("	 * @param ids");
            sw.WriteLine("	 * @return");
            sw.WriteLine("	 */");
            sw.WriteLine("	@PostMapping(\"batchRemove\")");
            sw.WriteLine("	@ResponseBody");
            sw.WriteLine("	public R batchRemove(String ids) {");
            sw.WriteLine("		List<String> idList=WebplusFormater.separatStringToList(ids);");
            sw.WriteLine("		boolean result = " + strTableNameLower + "Service.deleteBatchIds(idList);");
            sw.WriteLine("		if (result) {");
            sw.WriteLine("			return R.ok();");
            sw.WriteLine("		} else {");
            sw.WriteLine("			return R.error(\"删除失败\");");
            sw.WriteLine("		}");
            sw.WriteLine("	}");
            sw.WriteLine("");
            sw.WriteLine("}");
            sw.WriteLine("");


            sw.Close();

        }

        
    }
}
